﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.Administration;

namespace CKS.SharePoint.Workbook.Community {
    public enum ActivityType {
        UserAddedContent,
        UserJoinedCommunity
    }

    public struct ActivityLogConstants {
        public static Guid UserField = new Guid("{C22FF991-BC68-4669-9300-DD80A0AC8800}");
        public static Guid ActivityTypeField = new Guid("{57EFBB12-5D79-491F-87C0-B70C38780A97}");
        public static Guid MessageField = new Guid("{7CCBDE03-CE61-4077-A402-7C4437F699FC}");
        public static Guid DateField = new Guid("{9DF55927-BFA4-4D61-A217-5DFBB8B7829C}");
        public static Guid URLField = new Guid("{DD0529D9-265D-4797-8AF0-063BA2CE43D5}");
        public static string ActivityLogEventReceiverClass = "CKS.SharePoint.Workbook.Community.GenericListActivityEventReceiver";
    }

    public class ActivityLogger {
        private SPWeb _web;

        public ActivityLogger(SPWeb web) {
            _web = web;
        }

        public void Write(string message, ActivityType activity) {
            Write(message, activity, _web.CurrentUser);
        }

        public void Write(string message, ActivityType activity, SPUser user) {
            WriteActivityLog(message, activity, user, null);
        }

        public void Write(string message, ActivityType activity, SPUser user, string url) {
            WriteActivityLog(message, activity, user, url);
        }


        private void WriteActivityLog(string message, ActivityType activity, SPUser user, string url) {
            DateTime time = DateTime.Now;
            WriteActivityLogWithElevatedPrivileges(message, activity, user, url, time);
        }

        private void WriteActivityLogWithElevatedPrivileges(string message, ActivityType activity, SPUser user, string url, DateTime time) {
            SPSecurity.RunWithElevatedPrivileges(delegate {
                using (SPSite epSite = new SPSite(_web.Url)) {
                    using (SPWeb epWeb = epSite.OpenWeb()) {
                        SPList activityList = epWeb.Lists["ActivityLog"];
                        SPListItem newItem = activityList.Items.Add();
                        newItem[ActivityLogConstants.ActivityTypeField] = activity;
                        newItem[ActivityLogConstants.UserField] = user;
                        newItem[ActivityLogConstants.DateField] = time;
                        SPFieldUrlValue urlValue = new SPFieldUrlValue();
                        urlValue.Url = url;
                        urlValue.Description = url;
                        newItem[ActivityLogConstants.URLField] = urlValue.ToString();
                        newItem[ActivityLogConstants.MessageField] = message;
                        newItem.Update();
                    }
                }
            });
        }
    }
}